package ar.com.fiuba.ElectronicCircuitAnalyst.infrastructure.db4o.config;

import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Circuit;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Component;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;

public class ObjectContainerFactory{

	private String dataBaseName;
	
	private static ObjectContainer db;
	
	public static ObjectContainer getdefaultObjectContainer() {
		if ( db == null){
			db = Db4oEmbedded.openFile(getDefaultConfig(), "database.db4o");
		}
		return db;
	}
	
	private static EmbeddedConfiguration getDefaultConfig(){
		EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
		config.common().objectClass(Circuit.class).cascadeOnUpdate(false);
		config.common().objectClass(Circuit.class).cascadeOnDelete(false);
		config.common().objectClass(Component.class).cascadeOnUpdate(false);
		config.common().objectClass(Component.class).cascadeOnDelete(false);
		config.common().exceptionsOnNotStorable(true);
		return config;
	}

	/**
	 * @return the dataBaseName
	 */
	public String getDataBaseName() {
		return dataBaseName;
	}

	/**
	 * @param dataBaseName the dataBaseName to set
	 */
	public void setDataBaseName(String dataBaseName) {
		this.dataBaseName = dataBaseName;
	}

}
